package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
@SuppressWarnings("all")
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService roomGraphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper roomGraphInfoMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //todo 判断是否是更新操作
        boolean isUpdate = roomSubmitVo.getId() != null;
        this.saveOrUpdate(roomSubmitVo);
        //todo 实现保存或更新房间信息
        //todo 1. 如果是更新操作，需要先删除原有数据，再新增新数据
        //todo 获取roomId
        Long roomId = roomSubmitVo.getId();
        if (isUpdate) {
            //todo 删除图片、属性列表、配套设施、标签、支付方式、可选租期列表
            //todo 1.1 删除图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoWrapper = new LambdaQueryWrapper<>();
            graphInfoWrapper.eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, roomId);
            roomGraphInfoService.remove(graphInfoWrapper);
            //todo 1.2 删除配套 设施信息
            LambdaQueryWrapper<RoomFacility> roomFacilityWrapper = new LambdaQueryWrapper<>();
            roomFacilityWrapper.eq(RoomFacility::getRoomId, roomId);
            roomFacilityService.remove(roomFacilityWrapper);
            //todo 1.3 删除 标签信息
            LambdaQueryWrapper<RoomLabel> roomLabelWrapper = new LambdaQueryWrapper<>();
            roomLabelWrapper.eq(RoomLabel::getRoomId, roomId);
            roomLabelService.remove(roomLabelWrapper);
            //todo 1.4 删除属性信息
            LambdaQueryWrapper<RoomAttrValue> attrValueWrapper = new LambdaQueryWrapper<>();
            attrValueWrapper.eq(RoomAttrValue::getRoomId, roomId);
            roomAttrValueService.remove(attrValueWrapper);
            //todo 1.5 删除可选租期列表
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
            roomLeaseTermService.remove(roomLeaseTermWrapper);
            //todo 1.6 删除支付方式信息
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeWrapper.eq(RoomPaymentType::getRoomId, roomId);
            roomPaymentTypeService.remove(roomPaymentTypeWrapper);
        }
        //todo 2. 保存图片、属性列表、配套设施、标签、支付方式、可选租期列表
        //todo 2.1 保存图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphInfo graphVo : graphInfoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            roomGraphInfoService.saveBatch(graphInfoList);
        }
        //todo 2.2 保存属性列表信息
        List<Long> attrValueIdList = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIdList)) {
            ArrayList<RoomAttrValue> roomAttrValueList = new ArrayList<>();
            for (Long attrValueId : attrValueIdList) {
                RoomAttrValue roomAttrValue = RoomAttrValue
                        .builder().roomId(roomId).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //todo 2.3 保存配套设施信息
        List<Long> facilityIdList = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityIdList)) {
            ArrayList<RoomFacility> roomFacilityList = new ArrayList<>();
            for (Long facilityId : facilityIdList) {
                RoomFacility roomFacility = RoomFacility
                        .builder().roomId(roomId).facilityId(facilityId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //todo 2.4 保存标签信息
        List<Long> labelIdList = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelIdList)) {
            ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
            for (Long labelId : labelIdList) {
                RoomLabel roomLabel = RoomLabel
                        .builder().roomId(roomId).labelId(labelId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //todo 2.5 保存支付方式信息
        List<Long> paymentTypeIdList = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIdList)) {
            ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIdList) {
                RoomPaymentType roomPaymentType = RoomPaymentType
                        .builder().roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //todo 2.6 保存可选租期列表
        List<Long> leaseTermIdList = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIdList)) {
            ArrayList<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
            for (Long leaseTermId : leaseTermIdList) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm
                        .builder().roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }

    }

    @Override
    public IPage<RoomItemVo> selectRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.selectRoomItemVoPage(page, queryVo);
    }

    @Override
    public RoomDetailVo selectRoomDetailById(Long roomId) {
        //todo 1. 根据roomId 查询房间信息
        RoomInfo roomInfo = roomInfoMapper.selectById(roomId);
        //todo 2. 根据 房间所对应的公寓id 查询公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //todo 3. 查询房间所对应的 图片信息
        List<GraphVo> roomGraphVos = roomGraphInfoMapper.selectGraphVoListByItemTypeAndItemId(ItemType.ROOM, roomId);
        //todo 4. 查询房间所对应的 配套信息
        List<FacilityInfo> roomFacilityInfoList = roomFacilityMapper.selectRoomFacilityById(roomId);
        //todo 5. 查询房间所对应的 标签信息
        List<LabelInfo> roomLabelInfoList = roomLabelMapper.selectRoomLabelById(roomId);
        //todo 6. 查询房间所对应的 属性信息
        List<AttrValueVo> roomAttrValueList = roomAttrValueMapper.selectRoomAttrValueById(roomId);
        //todo 7. 查询房间所对应的 租期信息
        List<LeaseTerm> roomLeaseTermList = roomLeaseTermMapper.selectRoomLeaseTermById(roomId);
        //todo 8. 查询房间所对应的 支付方式信息
        List<PaymentType> roomPaymentTypeList = roomPaymentTypeMapper.selectRoomPaymentTypeById(roomId);
        //todo 9. 封装数据返回
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(roomGraphVos);
        roomDetailVo.setFacilityInfoList(roomFacilityInfoList);
        roomDetailVo.setLabelInfoList(roomLabelInfoList);
        roomDetailVo.setAttrValueVoList(roomAttrValueList);
        roomDetailVo.setLeaseTermList(roomLeaseTermList);
        roomDetailVo.setPaymentTypeList(roomPaymentTypeList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long id) {
        //todo 1. 删除房间信息
        super.removeById(id);
        //todo 根据id 删除图片、属性列表、配套设施、标签、支付方式、可选租期列表
        //todo 1.1 删除图片信息
        LambdaQueryWrapper<GraphInfo> roomGraphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomGraphInfoLambdaQueryWrapper
                .eq(GraphInfo::getItemType, ItemType.ROOM)
                .eq(GraphInfo::getItemId, id);
        roomGraphInfoService.remove(roomGraphInfoLambdaQueryWrapper);
        //todo 1.2 删除配套设施信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //todo 1.3 删除标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //todo 1.4 删除属性信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //todo 1.5 删除可选租期列表
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //todo 1.6 删除支付方式信息
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
    }


}




